package com.example.animatordemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView dudu_1;
	private AnimationDrawable mLoadingAnim;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		View iv_dudu = findViewById(R.id.iv_dudu);
		View iv_xian = findViewById(R.id.iv_xian);
		dudu_1 = (ImageView) findViewById(R.id.iv_dudu_1);
		dudu_1.setBackgroundResource(R.anim.page_loading);
		mLoadingAnim = (AnimationDrawable) dudu_1.getBackground();
		togetherRun(iv_dudu, iv_xian);
	}

	private void togetherRun(final View iv_dudu, final View iv_xian) {
		ViewTreeObserver observer = iv_dudu.getViewTreeObserver();
		observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				float y = iv_dudu.getY();
				Log.d("MainActivity", "" + y);
				ObjectAnimator anim1 = ObjectAnimator.ofFloat(iv_dudu, "Y", y, y - 36);
				ObjectAnimator anim2 = ObjectAnimator.ofFloat(iv_xian, "scaleX", 1.0f, 0.44f);
				anim1.setRepeatMode(ObjectAnimator.REVERSE);
				anim1.setRepeatCount(150000);
				anim2.setRepeatMode(ObjectAnimator.REVERSE);
				anim2.setRepeatCount(150000);
				AnimatorSet animSet = new AnimatorSet();
				animSet.setDuration(420);
				animSet.setInterpolator(new DecelerateInterpolator ());
				// 两个动画同时执行
				animSet.playTogether(anim1, anim2);
				animSet.start();
				mLoadingAnim.start();
				iv_dudu.getViewTreeObserver().removeGlobalOnLayoutListener(this);
			}
		});

	}

}
